package com.example.s.Activity;

import androidx.annotation.RequiresApi;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.s.R;
import com.example.s.adapter.InnerHolder;
import com.example.s.adapter.MyRecyclerViewAdapter;
import com.example.s.bean.RecContent;
import com.example.s.utils.LiveDataBus;
import com.example.s.view.MyNav;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class UserDetailActivity extends BaseActivity {
    private ImageView back;
    private CircleImageView headphoto;
    private TextView userName;
    private TextView introduction;
    private TextView label;
    private TextView likesAndCollctions;
    private TextView fans;
    private TextView follows;
    private Button follow;
    private TabLayout nav;
    private ViewPager vp;
    private MyNav<List<RecContent>, RecContent> listRecContentMyNav;
    private List<List<RecContent>> mWorksList = new ArrayList<>();

    private void initView() {
        back = (ImageView) findViewById(R.id.back);
        headphoto = (CircleImageView) findViewById(R.id.headphoto);
        userName = (TextView) findViewById(R.id.userName);
        introduction = (TextView) findViewById(R.id.introduction);
        label = (TextView) findViewById(R.id.label);
        likesAndCollctions = (TextView) findViewById(R.id.likesAndCollctions);
        fans = (TextView) findViewById(R.id.fans);
        follows = (TextView) findViewById(R.id.follows);
        follow = (Button) findViewById(R.id.follow);
        nav = (TabLayout) findViewById(R.id.nav);
        vp = (ViewPager) findViewById(R.id.vp);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setStatusBarColor(getResources().getColor(R.color.purple_bg));
        setContentView(R.layout.activity_user_detail);
        initView();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
headphoto.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        LiveDataBus.getInstance().with("currentHeadPhoto", Integer.class).setValue(R.drawable.yywtx1);
        startActivity(new Intent(UserDetailActivity.this,UserHeadPhotoDetail.class));
    }
});
        ArrayList<RecContent> work1 = new ArrayList<>();
        ArrayList<RecContent> work2 = new ArrayList<>();
        LiveDataBus.getInstance().with("currentUser", RecContent.class).observe(this, new Observer<RecContent>() {
            @Override
            public void onChanged(RecContent recContent) {
                headphoto.setImageResource(recContent.getHeadPhoto());
                userName.setText("小老板");
                fans.setText(String.valueOf(101));
                follows.setText(String.valueOf(0));
                likesAndCollctions.setText(String.valueOf(62));
                work1.add(recContent);
                mWorksList.add(work1);
                mWorksList.add(work2);
                listRecContentMyNav.setVpRecyclerData(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL), mWorksList, R.layout.rec_recycler_item);
            }
        });


        ArrayList<MyNav.NavItem> navItems = new ArrayList<>();
        navItems.add(new MyNav.NavItem("作品 1"));
        navItems.add(new MyNav.NavItem("喜欢 0"));

        listRecContentMyNav = new MyNav<List<RecContent>, RecContent>(nav, vp, navItems, new MyNav.OnMyNavListener() {
            @Override
            public void onCommonListener(ImageView icon, TextView title) {
                title.setTextColor(Color.parseColor("#FFB5B5B5"));
                title.setTextSize(16);
            }

            @Override
            public void onSelectListener(ImageView icon, TextView title, int pos) {
                title.setTextColor(Color.parseColor("#ffffff"));
            }
        })
                .setRecyclerCallBack(new MyNav.OnRecyclerCallBack<RecContent>() {
                    @Override
                    public void handleRecyclerData(RecyclerView view, MyRecyclerViewAdapter<RecContent> adapter, InnerHolder holder, RecContent item) {
                        Log.d(TAG, "handleData: " + item);
                        holder.setSrcResource(R.id.headphoto, item.getHeadPhoto());
                        holder.setSrcResource(R.id.contentPhoto, item.getContentPhoto());
                        holder.setText(R.id.title, item.getTitle());
                        holder.setText(R.id.subtitle, item.getSubTitle());
                        holder.setText(R.id.likes, String.valueOf(item.getLikes()));
                        holder.getView(R.id.follow).setVisibility(View.INVISIBLE);
                        holder.setOnClickListener(R.id.like, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                //点赞操作
                            }
                        });
//                        holder.setOnClickListener(R.id.headphoto, new View.OnClickListener() {
//                            @Override
//                            public void onClick(View v) {
//                                LiveDataBus.getInstance().with("currentUser", RecContent.class).setValue(item);
//                                Intent intent = new Intent(UserDetailActivity.this, UserDetailActivity.class);
//                                startActivity(intent);
//                            }
//                        });
                        holder.setOnClickListener(R.id.contentPhotoContainer, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                LiveDataBus.getInstance().with("currentWorkDetail", RecContent.class).setValue(item);
                                Intent intent = new Intent(UserDetailActivity.this, WorksDetailActivity.class);
                                startActivity(intent);
                            }
                        });
                    }
                });


    }


}
